﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Delivery_InvoiceList : INTPage
{
    SqlHelp help = new SqlHelp();
    CommonMethod comHelp = new CommonMethod();

    protected void Page_Load(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCode(this, CommonModel.errorTimeOutUrl1);
        
        if (!IsPostBack)
        {
            // 页面权限
            comHelp.GetRight(this, new string[] { Session["UserCode"].ToString(), Session["CoCode"].ToString(), "InvoiceList" }, Grv, new string[] { }, new int[] { Grv.Columns.Count-1 }, CommonModel.loginOut1);

            QueryBind(0);
            DdlBind();
        }
        CommonMethod.regAlertMessageScript(UpdHead, "deliveryOnloadApply();", this);
    }


    public void QueryBind(int n)
    {
        string sql=" select	distinct top 500 InvoiceNo, InvoiceDate, InvoiceAmount, InvoiceDesc from	M_InvoiceNo where DeleteFlag=0 ";
        if (n == 1)
        {
            if (!string.IsNullOrEmpty(txtDateFrom.Value) && !string.IsNullOrEmpty(txtDateTo.Value))
            {
                sql += string.Format(" and convert(datetime,InvoiceDate) between convert(datetime,'{0}')  and  convert(datetime,'{1}')",
                    txtDateFrom.Value, txtDateTo.Value);
            }
            else if (!string.IsNullOrEmpty(txtDateFrom.Value))
            {
                sql += string.Format(" and convert(datetime,InvoiceDate)>=convert(datetime,'{0}') ", txtDateFrom.Value);
            }
            else if (!string.IsNullOrEmpty(txtDateTo.Value))
            {
                sql += string.Format(" and convert(datetime,InvoiceDate)<=convert(datetime,'{0}') ", txtDateTo.Value);
            }
            if (!string.IsNullOrEmpty(txtContractNo.Text))
            {
                sql += string.Format(" and InvoiceNo in (select distinct InvoiceNo from M_InvoiceContract where ContractNoMain like '%{0}%') ",
                    txtContractNo.Text);
            }
            if (!string.IsNullOrEmpty(txtSaleName.Text))
            {
                sql += string.Format(" and InvoiceNo in (select distinct InvoiceNo from M_InvoiceContract "
                    + " where ContractNoMain in (select ContractNoMain from M_SalesCatalog where SalesName like '%{0}%')) ",
                    txtSaleName.Text);
            }
            if (!string.IsNullOrEmpty(txtCustomer.Text))
            {
                sql += string.Format(" and InvoiceNo in (select distinct InvoiceNo from M_InvoiceContract "
                    + " where ContractNoMain in (select ContractNoMain from M_Customer where CustomerName like '%{0}%')) ",
                    txtSaleName.Text);
            }
            if (!string.IsNullOrEmpty(txtInvoiceNo.Text))
            {
                sql += string.Format(" and InvoiceNo like '%{0}%' ", txtInvoiceNo.Text);
            }
            if (!string.IsNullOrEmpty(txtSoNo.Text))
            {
                sql += string.Format(" and InvoiceNo in (select distinct InvoiceNo from M_InvoiceContract where SONo like '%{0}%') ",
                    txtSoNo.Text);
            }
        }
        sql += " order	by InvoiceDate DESC";

        DataTable Dt = help.GetDataTable(sql);
        Grv.DataSource = Dt;
        Grv.DataBind();
        if (Dt.Rows.Count > 0)
        {
            // 设置分页行永远显示
            Grv.BottomPagerRow.Visible = true;
            //赋值Label记录条数
            Label label1 = Grv.BottomPagerRow.FindControl("lblCount") as Label;
            label1.Text = Dt.Rows.Count.ToString();
        }
    }

    protected void cmdSearch_Click(object sender, EventArgs e)
    {
        QueryBind(1);
        HidQueryFlag.Value = "1";
        CommonMethod.regAlertMessageScript(UpdHead, "deliveryOnloadApply();", this);
    }
    protected void cmdReset_Click(object sender, EventArgs e)
    {
        txtDateFrom.Value = "";
        txtDateTo.Value = "";
        HidQueryFlag.Value = "0";
        CommonMethod.regAlertMessageScript(UpdHead, "deliveryOnloadApply();", this);
    }

    protected void Grv_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            Grv.PageIndex = e.NewPageIndex;
            QueryBind(Convert.ToInt16(HidQueryFlag.Value.ToString()));
        }
        catch { }
    }
    protected void Grv_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "NewPage")
            {
                TextBox tb = Grv.BottomPagerRow.FindControl("txtNewPageIndex") as TextBox;
                int pageindex = Convert.ToInt32(tb.Text);
                Grv.PageIndex = pageindex - 1;
                //数据绑定
                QueryBind(Convert.ToInt16(HidQueryFlag.Value.ToString()));
            }
        }
        catch
        {
            CommonMethod.regAlertMessage("输入不合法-Error format!", this);
        }
    }
    protected void Grv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //鼠标经过事件
        if (e.Row.RowIndex != -1)
        {
            //设置悬浮鼠标指针形状为"小手"
            e.Row.Attributes["style"] = "Cursor:hand";
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "lastColor=this.style.backgroundColor;this.style.backgroundColor='#CCCC99';");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=lastColor;");
                ((LinkButton)e.Row.Cells[Grv.Columns.Count - 1].Controls[0]).Attributes.Add("onclick", "return confirm('确实要删除？');");
            }
        }
    }
    protected void Grv_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        CommonMethod.IsTimeOutUserCodeAndCoCode(this, CommonModel.errorTimeOutUrl1);

        string invoiceNo = Grv.DataKeys[e.RowIndex]["InvoiceNo"].ToString();

        string StrDelete = string.Format("delete from M_InvoiceContract where InvoiceNo='{0}'; Delete from M_InvoiceNo where InvoiceNo='{0}'", invoiceNo);
        if (help.RunSqlInt(StrDelete) > 0)
        {
            CommonMethod.regAlertMessage("success！", this);
            QueryBind(Convert.ToInt16(HidQueryFlag.Value.ToString()));
        }

        else
        {
            CommonMethod.regAlertMessage(" failed！", this);
        }
    }

    public void DdlBind()
    {
        ListItem item = new ListItem("请选择(Select Please)", "-1");
        DataTable[] dt = help.RunProcGetTables("GetddlForInvoiceListQuery", null);
        ddlContractNo.DataSource = dt[0];
        ddlContractNo.DataValueField = "ContractNoMain";
        ddlContractNo.DataTextField = "ContractNoMain";
        ddlContractNo.DataBind();
        ddlContractNo.Items.Insert(0, item);

        ddlContractNoCustomer.DataSource = dt[4];
        ddlContractNoCustomer.DataValueField = "ContractNoMain";
        ddlContractNoCustomer.DataTextField = "ContractNoCust";
        ddlContractNoCustomer.DataBind();
        ddlContractNoCustomer.Items.Insert(0, item);

        ddlSoNo.DataSource = dt[1];
        ddlSoNo.DataValueField = "ContractNoMain";
        ddlSoNo.DataTextField = "SONo";
        ddlSoNo.DataBind();
        ddlSoNo.Items.Insert(0, item);

        ddlSaleName.DataSource = dt[2];
        ddlSaleName.DataValueField = "SalesName";
        ddlSaleName.DataTextField = "SalesName";
        ddlSaleName.DataBind();
        ddlSaleName.Items.Insert(0, item);

        ddlCustomer.DataSource = dt[3];
        ddlCustomer.DataValueField = "CustomerName";
        ddlCustomer.DataTextField = "CustomerName";
        ddlCustomer.DataBind();
        ddlCustomer.Items.Insert(0, item);
    }
    protected void ddlContractNo_SelectedIndexChanged(object sender, EventArgs e)
    {
        string contractNo = ddlContractNo.SelectedValue.ToString();
        if (!contractNo.Equals("-1"))
        { txtContractNo.Text = contractNo; }
        else
        { txtContractNo.Text = ""; }
    }
    protected void ddlSoNo_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (!ddlSoNo.SelectedValue.Equals("-1"))
        { txtSoNo.Text = ddlSoNo.SelectedItem.Text; }
        else
        { txtSoNo.Text = ""; }
    }
    protected void ddlCustomer_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (!ddlCustomer.SelectedValue.Equals("-1"))
        { txtCustomer.Text = ddlCustomer.SelectedItem.Text; }
        else
        { txtCustomer.Text = ""; }
    }
    protected void ddlSaleName_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (!ddlSaleName.SelectedValue.Equals("-1"))
        { txtSaleName.Text = ddlSaleName.SelectedItem.Text; }
        else
        { txtSaleName.Text = ""; }
    }
    protected void ddlContractNoCustomer_TextChanged(object sender, EventArgs e)
    {
        if (!ddlContractNoCustomer.SelectedValue.Equals("-1"))
        { txtContractNoCustomer.Text = ddlContractNoCustomer.SelectedItem.Text; }
        else
        { txtContractNoCustomer.Text = ""; }
    }
}